﻿package proeva;

public class DDoS_H {
	private boolean type;//isUDP
	private double bitrate;
	private double requestRate;
	private double requestSize;
	private int qlimit;
	private double hitRatio_Real;
	private double aveResTime_Real;
	private double hitRatio_Predict;
	private double aveResTime_Predict;
	private double eva;
	private double fileSize;
	
	public void setType(boolean type) {
		this.type = type;
	}
	private boolean isType() {
		return type;
	}
	public void setBitrate(double bitrate) {
		this.bitrate = bitrate;
	}
	private double getBitrate() {
		return bitrate;
	}
	public void setRequestRate(double requestRate) {
		this.requestRate = requestRate;
	}
	private double getRequestRate() {
		return requestRate;
	}
	public void setRequestSize(double requestSize) {
		this.requestSize = requestSize;
	}
	private double getRequestSize() {
		return requestSize;
	}
	public void setQlimit(int qlimit) {
		this.qlimit = qlimit;
	}
	private int getQlimit() {
		return qlimit;
	}
	public void setHitRatio_Real(double hitRatio_Real) {
		this.hitRatio_Real = hitRatio_Real;
	}
	private double getHitRatio_Real() {
		return hitRatio_Real;
	}
	public void setAveResTime_Real(double aveResTime_Real) {
		this.aveResTime_Real = aveResTime_Real;
	}
	private double getAveResTime_Real() {
		return aveResTime_Real;
	}
	private void setHitRatio_Predict(double hitRatio_Predict) {
		this.hitRatio_Predict = hitRatio_Predict;
	}
	public double getHitRatio_Predict() {
		return hitRatio_Predict;
	}
	private void setAveResTime_Predict(double aveResTime_Predict) {
		this.aveResTime_Predict = aveResTime_Predict;
	}
	public double getAveResTime_Predict() {
		return aveResTime_Predict;
	}
	private void setEva(double eva) {
		this.eva = eva;
	}
	public double getEva() {
		return eva;
	}
	
	public void setFileSize(double fileSize) {
		this.fileSize = fileSize;
	}
	private double getFileSize() {
		return fileSize;
	}
	
	//归一化函数
	private double Normalize( double Max, double Min, double data )
	{
		return ( data - Min ) / ( Max - Min );
	}
	
	//反归一化函数
	private double DeNormalize( double Max, double Min, double data )
	{
		return  data * (Max - Min) + Min;
	}
	
	private double tansig( double data )
	{
		if( Math.exp(-2 * data) >= Double.POSITIVE_INFINITY )
			return -1;
		else if( Math.exp(-2 * data) <= Double.NEGATIVE_INFINITY )
			return 1;
		else
			return ( 1 - Math.exp( -2*data )) / ( 1 + Math.exp( -2*data ));
	}
	
	public void CalcEva_udp()
	{
		/////////////////////////初始化
		//隐层
		double[] Y = new double[11];
		//输出层
		double[] O = {0.0, 0.0};
		//输入层到隐层的权重矩阵 4行11列
		double [][] V =  { 
				{ -39.685651573732777,  -1.287674387722133, -11.602505223075495,  -5.966676584520355,  -9.144345924432155,   6.335817297839115,  15.203174477230370,   6.212465954639501,  -0.168502759348013,  -1.388805297542378,  -1.180470141862861},
				{ -71.408142388545102, -15.925102591724432,   6.752110322861114,  28.183779118247838, -66.478602532072017,   7.057922918900195,  -7.800669091643934, -19.963913298051658,   6.246361593339842,   6.405927628601599,  29.017265699407083},
				{-42.836421704511231,   3.880590979572151,  -7.510399764822848,  11.078650770361902, -32.627595626962560, -13.849052866383436,  -3.146303009352818,  11.964354235937595,   2.808358973806385,   0.416223993719578, -15.911148842944721},
				{ 11.236179120063440, -19.357427241071001,   7.302149201676789, -14.532355556804836,   3.029426328750773,  -6.199107062811689,  -4.101610772024333,  -4.161319994989507,   2.774800251930742,  -3.247048011037045,  15.053580815170127}
			};
		//隐层到输出层的权重矩阵 11行2列
		double [][] W = {
				  {-1.333580168163813,   0.514222641741241},
				  {-2.005858004269565,   1.076385906501971},
				   {0.281452136571242,  -0.218519042194978},
				   {2.004662370133411,  -0.895875567796572},
				   {1.207706277490023,  -0.443214130679584},
				  {-2.338489584352351,   1.060371956450424},
				   {0.919032250457895,  -0.495096692242330},
				   {2.299843134964628,  -1.133506443746682},
				  {-3.165290733843400,   1.464943349460925},
				   {4.487784284349599,  -1.719300222454955},
				   {4.438801212639605,  -2.012294209404804}
			};
		//输入层到隐层的阈值向量 11个
		double [] T = {  83.453061359749071,
				  22.323288351576714,
				   1.669412979266638,
				   0.097787376069889,
				  42.351563596607306,
				   4.685346380876648,
				   4.719604347854747,
				  -0.454922667297741,
				  -5.636154345092494,
				  -5.149425134678078,
				   2.501305467839805};
		//隐层到输出层的阈值向量
		double [] H = {1.344523801448371, 0.058443707581098};
		//输入最大值
		double [] Max = { 100, 284.764, 10000, 5000000, 0.999884, 318.187 };
		//输入的最小值
		double [] Min = { 1, 0.802, 100, 500000, 0.090909, 0.11447 };
		////////////////////////归一化
		double bitrate_n = Normalize( Max[0], Min[0], bitrate);
		double requestRate_n = Normalize( Max[1], Min[1], requestRate);
		double requestSize_n = Normalize( Max[2], Min[2], requestSize);
		double fileSize_n = Normalize( Max[3], Min[3], fileSize);
		
		////////////////////////预测
		//输入层到隐层的计算
		for ( int i = 0; i < 11 ; i++ )//列
		{
			Y[i] = bitrate_n * V[0][i] + requestRate_n * V[1][i] + requestSize_n * V[2][i] + fileSize_n * V[3][i] + T[i];
			Y[i] = tansig( Y[i] );
		}
		//隐层到输出层的计算
		for ( int i = 0; i < 2 ; i++ )//列
			for ( int j = 0 ; j < 11 ; j++ )
			{
				O[i] += Y[j] * W[j][i];
			}
		O[0] = O[0] + H[0];
		O[1] = O[1] + H[1];
		//////////////////////反归一化
		hitRatio_Predict = DeNormalize( Max[4], Min[4], O[0] );
		aveResTime_Predict = DeNormalize( Max[5], Min[5], O[1] );
		
		//计算防守绩效
		double E_p = hitRatio_Predict*aveResTime_Predict + hitRatio_Predict*1000;
		double E_r = hitRatio_Real*aveResTime_Real + hitRatio_Real*1000;
		
		eva = E_p - E_r;
	}
	
	public void CalcEva_SYN()
	{
		/////////////////////////初始化
		//隐层
		double[] Y = new double[11];
		//输出层
		double[] O = {0.0, 0.0};
		//输入层到隐层的权重矩阵  5行11列
		double [][] V = {
				{-0.079762564,0.004165385,-0.285750294,1.706009258,0.045827282,0.172210089,-0.759290201,-0.006835255,-4.377639091,-0.209964931,0.115178353},
				{-8.979914789,-3.503410526,-21.04026729,29.48926783,22.2757255,0.587993294,6.750295838,1.622800807,-5.013549507,-16.0173722,-0.365446441},
				{4.463896613,-0.999994377,6.438255276,-52.98231131,4.567379205,-11.61539333,-6.017298625,1.729534693,3.928607778,-20.91989849,-7.502693249},
				{0.000819437,0.003360629,-0.076607878,0.40203296,-0.990441634,-0.007116867,-2.130224478,-0.00270736,-1.389549731,8.167630752,-0.004981864},
				{-0.007470966,-0.004408839,0.038507005,0.208592758,-0.101434829,-0.032071508,21.36353394,0.007714329,17.74408017,0.688163654,-0.00907304}
		};
		//隐层到输出层的权重矩阵  11行2列
		double [][] W = {
				{0.71829104,-1.972511846},
				{4.343068283,-23.09595253},
				{-0.513745757,0.529280127},
				{-0.183452617,0.314718171},
				{2.907577951,-12.64784163},
				{0.100821193,2.111947168},
				{-0.02182691,0.043022793},
				{1.761089515,-13.12467644},
				{0.02695739,-0.034885784},
				{-0.12792963,0.317712625},
				{0.568702378,-5.178883297}

		};
		//输入层到隐层的阈值向量  11个
		double [] T = {6.539419783,3.310767663,-1.530058188,17.73369643,-19.5589328,5.974030042,-5.904598936,-2.178768341,1.148105731,-2.070931988,4.316986698};
		//隐层到输出层的阈值向量  2个
		double [] H = {-0.384517548, 3.434848347};
		
		//输入最大值
		double [] Max = { 20, 100, 6.7, 1000, 1500, 1, 0.5121 };
		//输入的最小值
		double [] Min = { 1, 20, 4.232, 20, 500, 0.1216, 0.2406 };
		////////////////////////归一化
		double bitrate_n = Normalize( Max[0], Min[0], bitrate);
		double qlimit_n = Normalize( Max[1], Min[1], qlimit);
		double requestRate_n = Normalize( Max[2], Min[2], requestRate);
		double requestSize_n = Normalize( Max[3], Min[3], requestSize);
		double fileSize_n = Normalize( Max[4], Min[4], fileSize);
		
		////////////////////////预测
		//输入层到隐层的计算
		for ( int i = 0; i < 11 ; i++ )//列
		{
			Y[i] = bitrate_n * V[0][i] + qlimit_n * V[1][i] + requestRate_n * V[2][i] + requestSize_n * V[3][i] + fileSize_n * V[4][i] + T[i];
			Y[i] = tansig( Y[i] );//传递函数
		}
		//隐层到输出层的计算
		for ( int i = 0; i < 2 ; i++ )//列
			for ( int j = 0 ; j < 11 ; j++ )
			{
				O[i] += Y[j] * W[j][i];
			}
		O[0] = O[0] + H[0];
		O[1] = O[1] + H[1];
		//////////////////////反归一化
		hitRatio_Predict = DeNormalize( Max[5], Min[5], O[0] );
		aveResTime_Predict = DeNormalize( Max[6], Min[6], O[1] );
		
		//计算防守绩效
		double E_p = hitRatio_Predict*aveResTime_Predict + hitRatio_Predict*1000;
		double E_r = hitRatio_Real*aveResTime_Real + hitRatio_Real*1000;
		
		eva = E_p - E_r;
	}
	
//	public static void main( String [] args )
//	{
	//bitrate=100&requestrate=10&requestsize=100&filesize=5000000&
	//qlimit=50&hitratio=1.1&averestime=1.2
//		DDoS_H test = new DDoS_H();
//		test.setBitrate(100);
//		test.setRequestRate(73.446);
//		test.setRequestSize(10000);
//		test.setFileSize(5000000);
//		
//		test.CalcEva_udp();
//		
//		test.setBitrate(20);
//		test.setQlimit(20);
//		test.setRequestRate(5.555);
//		test.setRequestSize(20);
//		test.setFileSize(2000);
//		test.setBitrate(100);
//		test.setQlimit(50);
//		test.setRequestRate(10);
//		test.setRequestSize(100);
//		test.setFileSize(5000000);
//		
//		test.CalcEva_SYN();
//		
//		System.out.println( test.getHitRatio_Predict() + "  " + test.getAveResTime_Predict() + " " + test.getEva() );
//	}

}
